package com.homedo.microservice.distributed.id.service.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.distributed.id.service.bean.po.LeafAllocExtraNo;
import com.homedo.microservice.distributed.id.service.bean.po.enhanced.LeafAllocExtraNoEnhanced;
import com.homedo.microservice.distributed.id.service.conf.LeafAllocExtraNoConf;
import com.homedo.microservice.distributed.id.service.exception.LeafServerException;
import com.homedo.microservice.distributed.id.service.persistence.dao.LeafAllocExtraNoDao;
import com.homedo.microservice.distributed.id.service.task.LeafAllocExtraNoConfUpdateTask;
import com.sankuai.inf.leaf.common.Result;
import com.sankuai.inf.leaf.common.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Random;

/**
 * @author 作者 jgshun
 * @date 创建时间 2019年03月15日 09时39分50秒
 */
@Service
public class LeafAllocExtraNoService extends BaseService<LeafAllocExtraNo> {
    private static final Logger _LOGGER = LoggerFactory.getLogger(LeafAllocExtraNoService.class);

    @Autowired
    private LeafAllocExtraNoDao leafAllocExtraNoDao;
    @Autowired
    private SegmentService segmentService;
    @Autowired
    private LeafAllocExtraNoConfUpdateTask leafAllocExtraNoConfUpdateTask;

    private static final Random RANDOM = new Random();

    @PostConstruct
    public void init() {
        leafAllocExtraNoConfUpdateTask.update();
    }

    @Override
    public List<LeafAllocExtraNoEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<LeafAllocExtraNo> leafAllocExtraNos = leafAllocExtraNoDao.getListByIds(ids, LeafAllocExtraNo.class);

        if (!Detect.notEmpty(leafAllocExtraNos)) {
            return null;
        }

        return this.convent2Enhanceds(leafAllocExtraNos);
    }


    @Override
    public List<LeafAllocExtraNoEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<LeafAllocExtraNo> leafAllocExtraNos = leafAllocExtraNoDao.getListByIdsGreaterThanMark0(ids, LeafAllocExtraNo.class);

        if (!Detect.notEmpty(leafAllocExtraNos)) {
            return null;
        }

        return this.convent2Enhanceds(leafAllocExtraNos);
    }


    @Override
    public List<LeafAllocExtraNoEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<LeafAllocExtraNo> leafAllocExtraNos = (List<LeafAllocExtraNo>) pos;
        List<LeafAllocExtraNoEnhanced> leafAllocExtraNoEnhanceds = Lists.newArrayList();
        leafAllocExtraNos.forEach(e -> leafAllocExtraNoEnhanceds.add(new LeafAllocExtraNoEnhanced(e)));
        return leafAllocExtraNoEnhanceds;
    }


    public String generateNo(String bizCode) {
        Integer bizCodeInt = Integer.valueOf(bizCode);
        LeafAllocExtraNo leafAllocExtraNo = LeafAllocExtraNoConf.getInstance().getConfs().get(bizCodeInt);
        if (leafAllocExtraNo == null) {
            _LOGGER.error("[单号业务编码不存在:{}]", bizCode);
        }

        Result result = segmentService.getId(bizCode);

        if (result.getStatus().equals(Status.EXCEPTION)) {
            _LOGGER.error("[生成单号出错:{}--{}]", bizCode, result.toString());
            throw new LeafServerException(result.toString());
        }

        long sequence = RANDOM.nextInt(1024);

        StringBuilder strBud = new StringBuilder();
        strBud.append(leafAllocExtraNo.getPrefix()).append(result.getId() << 22 | sequence << 12 | bizCodeInt.longValue()).append(leafAllocExtraNo.getSuffix());

        return strBud.toString();
    }

}
